package com.example.last.controller;


import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.example.last.common.Result;
import com.example.last.entity.Notice;
import com.example.last.entity.Questions;
import com.example.last.entity.Params;
import com.example.last.service.QuestionsService;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@CrossOrigin
@RequestMapping("/questions")
@RestController
public class QuestionsController {
    @Resource
    private QuestionsService questionsService;
    //图书信息查询
    @GetMapping("/search")
    public Result findBySearch(@Param("params") Params params){
        PageInfo<Questions> info = questionsService.findBySearch(params);
        return Result.success(info);
    }
    //新增图书信息
    @PostMapping
    public Result save(@RequestBody Questions questions){
        if(questions.getId() == null){
            questionsService.add(questions);
        }else{
            questionsService.update(questions);
        }
        return Result.success();
    }

    @GetMapping("/{typeId}")
    public Result getQuestion(@PathVariable Integer typeId){
        List<Questions> questionList = questionsService.findQuestionById(typeId);
        return Result.success(questionList);
    }

    //删除图书信息
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        questionsService.delete(id);
        return Result.success();
    }

    //视图
    @GetMapping("/echarts/bie")
    public Result bie(){
        //查询所有图书
        List<Questions> list = questionsService.findAll();
        //定义map转存list
        Map<String, Long> collect = list.stream()//将数据转为stream流
                .filter(x -> ObjectUtil.isNotEmpty(x.getTypeName()))//筛除空数据
                .collect(Collectors.groupingBy(Questions::getTypeName, Collectors.counting()));//分组封装数据

        //最后返回给前端的数据结构
        List<Map<String,Object>> mapList = new ArrayList<>();
        if(CollectionUtil.isNotEmpty(collect)){
            for(String key : collect.keySet()){
                Map<String, Object> map = new HashMap<>();
                map.put("name", key);
                map.put("value", collect.get(key));
                mapList.add(map);
            }
        }
        return Result.success(mapList);
    }

    @GetMapping("/echarts/bar")
    public Result bar(){
        //查询所有图书
        List<Questions> list = questionsService.findAll();
        //定义map转存list
        Map<String, Long> collect = list.stream()//将数据转为stream流
                .filter(x -> ObjectUtil.isNotEmpty(x.getTypeName()))//筛除空数据
                .collect(Collectors.groupingBy(Questions::getTypeName, Collectors.counting()));//分组封装数据

        List<String> xAxis = new ArrayList<>();
        List<Long> yAxis = new ArrayList<>();

        if(CollectionUtil.isNotEmpty(collect)){
            for(String key : collect.keySet()){
                xAxis.add(key);
                yAxis.add(collect.get(key));
            }
        }
        Map<String, Object> map = new HashMap<>();
        map.put("xAxis",xAxis);
        map.put("yAxis",yAxis);

        return Result.success(map);
    }

    //批量删除
    @PutMapping("/batch")
    public Result batchdel(@RequestBody List<Questions> list){
        for(Questions questions : list){
            questionsService.delete(questions.getId());
        }
        return Result.success();
    }


}
